package com.dl.mall;

import com.alibaba.fastjson.JSON;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.Product;
import com.dl.mall.info.BrandInfo;
import com.dl.mall.mapper.ProductMapper;
import com.dl.mall.service.user.ProductService;
import com.dl.mall.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-03-13 17:15
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class testProduct {

    @Resource
    ProductService productService;

    @Resource
    ProductMapper productMapper;

    @Test
    public void queryProduct(){

        PageHelper.startPage(2, 3);
        List<Product> list = productMapper.queryProduct();
        System.out.println("list = " + list);

        Page<Product> page = (Page<Product>) PageUtils.getPage(list);

        String string = JSON.toJSONString(page);
        System.out.println("string = " + string);
        System.out.println("page = " + page);
    }

    @Test
    public void productService(){
        Page<Product> xiaomi = productService.getProductByBrandId(BrandInfo.Phone_Xiaomi, 1, 4);

        String string = JSON.toJSONString(xiaomi);
        System.out.println("string = " + string);

        Page<Product> all = productService.getProductByBrandId(BrandInfo.Phone_Xiaomi, 1, 0);
        String string1 = JSON.toJSONString(all);
        System.out.println("string1 = " + string1);

    }



    /*===========下面是自定义分页=========*/

/*    @Test
    public void getProductByBrandId(){

        List<List<?>> list = productMapper.getProductByBrandId(101,0,10);


        List<Product> productList = (List<Product>) list.get(0);
        List<Integer> count = (List<Integer>) list.get(1);

        productList.forEach((item)->{
            System.out.println("item = " + item);
        });
        System.out.println("count = " + count.get(0));
    }*/

    @Test
    public void getProductByBrandIdService(){

        Page<Product> page = productService.getProductByBrandId(BrandInfo.Phone_Xiaomi, 0, 10);

        System.out.println("page = " + page);
    }
/*
    @Test
    public void getProductPage(){

        List<List<?>> list = productMapper.getProductPage(0,1);

        List<Product> productList = (List<Product>) list.get(0);
        List<Integer> count = (List<Integer>) list.get(1);

        productList.forEach((item)->{
            System.out.println("item = " + item);
        });
        System.out.println("count = " + count.get(0));
    }*/

}
